/**
 * 地图绑定事件的代码
 */
let ScreenSpaceEventType = Cesium.ScreenSpaceEventType
const EventCode = {
  leftClick: ScreenSpaceEventType.LEFT_CLICK,
  leftDoubleClick: ScreenSpaceEventType.LEFT_DOUBLE_CLICK,
  leftDown: ScreenSpaceEventType.LEFT_DOWN,
  leftUp: ScreenSpaceEventType.LEFT_UP,
  middleClick: ScreenSpaceEventType.MIDDLE_CLICK,
  middleDown: ScreenSpaceEventType.MIDDLE_DOWN,
  middleUp: ScreenSpaceEventType.MIDDLE_UP,
  mouseMove: ScreenSpaceEventType.MOUSE_MOVE,
  pinchEnd: ScreenSpaceEventType.PINCH_END,
  pinchMove: ScreenSpaceEventType.PINCH_MOVE,
  pinchStart: ScreenSpaceEventType.PINCH_START,
  rightClick: ScreenSpaceEventType.RIGHT_CLICK,
  rightDown: ScreenSpaceEventType.RIGHT_DOWN,
  rightUp: ScreenSpaceEventType.RIGHT_UP,
  wheel: ScreenSpaceEventType.WHEEL
}

/**
   *
   * Gets the mouse information for the mouse event
   * @param position
   * @private
   *
   */
function getMouseInfo(position) {
  let scene = viewer.scene
  let target = scene.pick(position)
  let cartesian = undefined
  let surfaceCartesian = undefined
  let wgs84Position = undefined
  let wgs84SurfacePosition = undefined
  if (scene.pickPositionSupported) {
    cartesian = scene.pickPosition(position)
  }
  if (cartesian) {
    let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian)
    if (c) {
      wgs84Position = {
        lng: Cesium.Math.toDegrees(c.longitude),
        lat: Cesium.Math.toDegrees(c.latitude),
        alt: c.height
      }
    }
  }
  if (
    scene.mode === Cesium.SceneMode.SCENE3D &&
    !(viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)
  ) {
    let ray = scene.camera.getPickRay(position)
    surfaceCartesian = scene.globe.pick(ray, scene)
  } else {
    surfaceCartesian = scene.camera.pickEllipsoid(
      position,
      Cesium.Ellipsoid.WGS84
    )
  }
  if (surfaceCartesian) {
    let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(surfaceCartesian)
    if (c) {
      wgs84SurfacePosition = {
        lng: Cesium.Math.toDegrees(c.longitude),
        lat: Cesium.Math.toDegrees(c.latitude),
        alt: c.height
      }
    }
  }

  return {
    target: target,
    windowPosition: position,
    position: cartesian,
    wgs84Position: wgs84Position,
    surfacePosition: surfaceCartesian,
    wgs84SurfacePosition: wgs84SurfacePosition
  }
}

const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(
  ''
)

/**
   * Generates uuid
   * @param prefix
   * @returns {string}
   */
function uuid(prefix = 'D') {
  let uuid = []
  uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  uuid[14] = '4'
  let r
  for (let i = 0; i < 36; i++) {
    if (!uuid[i]) {
      r = 0 | (Math.random() * 16)
      uuid[i] = CHARS[i === 19 ? (r & 0x3) | 0x8 : r]
    }
  }
  return prefix + '-' + uuid.join('')
}

function position2wgs84(item) {
  let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(item)
  if (c) {
    return {
      lng: Cesium.Math.toDegrees(c.longitude),
      lat: Cesium.Math.toDegrees(c.latitude),
      alt: c.height
    }
  }
  return null
}

export {
  getMouseInfo,
  EventCode,
  position2wgs84,
  uuid
}
